<?php 
require_once('../kelas/kategori.php');
require_once('../kelas/produk.php');
require_once('../kelas/tr_barang_masuk.php');
$detBM = $objTransBarangMasuk->detailTemp();
?>
<h3>Barang Masuk</h3>
<h4>Informasi Barang Masuk</h4>
<form method="POST" name="frmInfBarangMasuk">
    <table border="0" title="Informasi Barang Masuk">
        <tr>
            <td>Tanggal Barang Masuk</td>
            <td>
                <input type="text" name="date_bm" required="required" value="<?php echo date('Y-m-d') ?>" class="text_box_3">
            </td>
        </tr><tr>
            <td>Keterangan</td>
            <td>
                <textarea class="text_box_3" name="keterangan"></textarea>
            </td>
        </tr>
        <tr >
            <td colspan="2">
                <input type="submit"   <?php
if ($detBM['total'] == 0) {
    echo 'disabled="disabled" title="Belum ada barang yang ditambah"';
}
?> value="Simpan Transaksi"/>
                <input type="reset"    value="Reset"/>
            </td>
        </tr>
    </table>
</form>
<hr/>
<h4>Detail Barang</h4>
<form method="POST" action="<?php echo BASEURL . 'proses/tr_barang_masuk.php?on=cekBarang'; ?>" name="frmDetailBM">
    <table border="0" title="Tambah Detail Barang">
        <tr>
            <td>Produk</td>
            <td>
                <select class="text_box_3" name="idproduk"><?php echo $objProduk->getCombo(); ?></select>
            </td>
        </tr><tr>
            <td>Jumlah Masuk</td>
            <td>
                <input class="text_box_3" name="jumlah"/>
            </td>
        </tr>
        <tr >
            <td colspan="2">
                <input type="submit"    value="Tambah"/>
                <input type="reset"    value="Reset"/>
            </td>
        </tr>
    </table>
</form>
<div class="frame_table_order">
<table width="100%" cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">

    <thead>
        <tr align="center" valign="middle" bgcolor="#CCCCCC">
<th>No.</th><th>Nama Barang</th><th>Jumlah</th><th>Aksi</th></tr>    
    </thead>
    <tbody>
        <?php
        if ($detBM['total'] > 0) {
            $no = 1;
            foreach ($detBM['result'] as $vData) {
                ?>
                <tr class="font_isi_judul" align="center" valign="middle">
                    <td><?php echo $no++; ?></td>
                    <td><?php echo $vData['nama'] ?></td>
                    <td><?php echo $vData['jumlah'] ?></td>
                    <td><a href="<?php echo $vData['idproduk']; ?>" class="del_bmd option" >Hapus</a>
                        <a href="<?php echo $vData['idproduk']; ?>" jumlah="<?php echo $vData['jumlah']; ?>" class="edit_bmd option" >Edit</a>
                    </td>
                </tr><?php
    }
} else {
            ?><tr class="font_isi_judul" align="center" valign="middle">
            <td colspan="4">(belum ada barang yang ditambahkan)</td></tr><?php
    };
        ?>
    </tbody>
    <tfoot>
        <tr><th colspan="4">Detail Barang Masuk</th></tr>
    </tfoot>
</table>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        $(document).find('form[name="frmInfBarangMasuk"]').submit(function(e){
            var thisF =$(document).find('form[name="frmInfBarangMasuk"]');
            var cfSimpan = confirm('Simpan Transaksi Barang Masuk?');
            if(cfSimpan==true){
                $.ajax({
                    url:baseUrl+'proses/tr_barang_masuk.php?on=simpan',
                    dataType:'JSON',
                    data:thisF.serialize(),
                    type:"POST",
                    success:function(eS){
                        if(eS.success==true){
                            document.location = document.URL;
                        }
                        else{
                            alert(eS.error_msg);
                        }
                    },
                    error:function(eS){
                        alert("Gagal Simpan Transaksi Barag Masuk");
                    }
                    
                });
            }
            e.preventDefault();
        });
        function lanjutkan(idP,jml,resetP){
            $.ajax({
                url:baseUrl+'proses/tr_barang_masuk.php?on=simpandetail',
                dataType:'JSON',
                data:{
                    idproduk:idP,
                    jumlah:jml,
                    reset:resetP
                },
                type:'POST',
                success:function(resp){
                    if(resp.success==true){
                        document.location = document.URL;
                    }
                    else{
                        alert(resp.success.error_msg);
                    }
                    
                },
                error:function(resp){
                    alert('Gagal Memproses');
                }                       
            });
        };
        var dataDetail = <?php echo json_encode($objTransBarangMasuk->detailTemp()); ?>;
        console.log(dataDetail);
        $('a.del_bmd').click(function(r){
            var idP = r.currentTarget.getAttribute('href');
            $.ajax({
                url:baseUrl+'proses/tr_barang_masuk.php?on=hapusdetail',
                type:'POST',
                dataType:'JSON',
                data:{
                    idproduk:idP
                },
                success:function(resp){
                    if(resp.success==true){
                        document.location =document.URL;
                    }
                    else{
                        alert(resp.error_msg);
                    }
                    
                },
                error:function(){
                    alert('Gagal Menghapus Data');
                }
                
            });
            r.preventDefault();
        });
        $('a.edit_bmd').click(function(evt){
            var jml = evt.currentTarget.getAttribute('jumlah');
            var idP= evt.currentTarget.getAttribute('href');
            masdo  = evt.currentTarget;
            var thisFormDetail = $(document).find('form[name=frmDetailBM]');
            thisFormDetail.find('select[name="idproduk"]').val(idP);            
            thisFormDetail.find('input[name="jumlah"]').val(jml);
            thisFormDetail.find('input[type="submit"]').val('Update');
            evt.preventDefault();            
        });            
        $(document).find('form[name=frmDetailBM]').find('input[type="reset"]').click(function(e){
            $(document).find('form[name=frmDetailBM]').find('input[type="submit"]').val('Tambah');    
            
        });
        $(document).find('form[name=frmDetailBM]').submit(function(e){
            var thisFormDetail = $(document).find('form[name=frmDetailBM]');
            var prod = thisFormDetail.find('select[name="idproduk"]').val();
            var jmlP= thisFormDetail.find('input[name="jumlah"]').val();
            var posisi = thisFormDetail.find('input[type="submit"]').val();
            var jml = parseInt(jmlP);
            var prodInt = parseInt(prod);
            if(prodInt>0){
                //do check
                if(jml>0){
                    var ada = false;
                    for(var i=0;i<dataDetail.result.length;i++){
                        if(dataDetail.result[i].idproduk==prodInt){
                            ada=true;
                        };
                    };
                    if(ada==false){
                        lanjutkan(prodInt, jml);
                    }
                    else{
                        var tombol = thisFormDetail.find('input[type="submit"]').val();
                        
                        if(tombol=='Tambah'){                            
                            var tanya = confirm('Barang Ini Telah Di input sebelumnya, ganti jumlahnya?');
                            if(tanya==true){
                                lanjutkan(prodInt, jml,1);
                            }
                        }
                        else{
                            lanjutkan(prodInt, jml,1);
                        }
                    }
                    
                }
                else{
                    alert('Jumlah Harus Angka dan Tidak Boleh Dari Nol');
                }
            }else{
                alert('Pilih Produk Dulu');
            };
            e.preventDefault();
        });
        
    });
</script>
<?php
//echo $fungsi?>